package billings.classtasks;

import java.util.List;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import billings.classtasks.classes.Class;
import billings.classtasks.classes.ClassDAO;
import billings.classtasks.classes.CreateClass;
import billings.classtasks.instructors.CreateInstructor;
import billings.classtasks.instructors.InstructorDAO;
import billings.classtasks.tasks.CreateTask;
import billings.classtasks.tasks.Task;
import billings.classtasks.tasks.TaskDAO;

public class ClassTasksActivity extends ExpandableListActivity implements OnClickListener {
    /** Called when the activity is first created. */
	private ClassDAO classDatasource;
	private TaskDAO taskDatasource;
	private InstructorDAO instructorDatasource;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        classDatasource = new ClassDAO(this);
        taskDatasource = new TaskDAO(this);
        instructorDatasource = new InstructorDAO(this);
        classDatasource.open();
        taskDatasource.open();
        instructorDatasource.open();
        
        if(getIntent() != null && getIntent().getExtras() != null && !getIntent().getExtras().isEmpty()){
        	Bundle extras = getIntent().getExtras();
        	
        	//see if a class should be created
        	if(extras.getBundle("classVals") != null && !extras.getBundle("classVals").isEmpty()){
	        	Bundle classBundle = extras.getBundle("classVals");
        		String courseNumber = classBundle.getString("courseNumber");
	        	String courseMajor = classBundle.getString("courseMajor");
	        	String courseProfessor = classBundle.getString("courseProfessor");
	        	
	        	if(extras.getString("action").equals("create")){
	        		classDatasource.createClass(courseNumber, courseMajor, courseProfessor);
	        	}else if(extras.getString("action").equals("update")){
	        		classDatasource.updateClass(classBundle.getInt("classId"), courseNumber, courseMajor, courseProfessor);
	        	}
        	}
        	
        	//see if a task should be created
        	if(extras.getBundle("taskVals") != null && !extras.getBundle("taskVals").isEmpty()){
	        	Bundle taskBundle = extras.getBundle("taskVals");
        		String title = taskBundle.getString("title");
	        	String description = taskBundle.getString("description");
	        	String dueDate = taskBundle.getString("dueDate");
	        	int classId = taskBundle.getInt("classId");
	        	
	        	if(extras.getString("action").equals("create")){
	        		taskDatasource.createTask(title, description, dueDate, classId);
	        	}else if(extras.getString("action").equals("update")){
	        		taskDatasource.updateTask(taskBundle.getInt("taskId"), description, title, dueDate, classId);
	        	}
        	}
        	
        	//see if an instructor should be created
        	if(extras.getBundle("instructorVals") != null && !extras.getBundle("instructorVals").isEmpty()){
        		Bundle instructorBundle = extras.getBundle("instructorVals");
        		
        		String instructorFirstName = instructorBundle.getString("instructorFirstName");
	        	String instructorLastName = instructorBundle.getString("instructorLastName");
	        	String instructorEmail = instructorBundle.getString("instructorEmail");
	        	String instructorPhone = instructorBundle.getString("instructorPhone");
	        	String instructorDepartment = instructorBundle.getString("instructorDepartment");
	        	String instructorOfficeBuilding = instructorBundle.getString("instructorOfficeBuilding");
	        	String instructorOfficeNumber = instructorBundle.getString("instructorOfficeNumber");
	        	int instructorTypeId = instructorBundle.getInt("instructorTypesId");
	        	int instructorId = instructorBundle.getInt("instructorId");
	        	
	        	if(extras.getString("action").equals("create")){
	        		instructorDatasource.createInstructor(instructorFirstName, instructorLastName, 
	        											  instructorDepartment, instructorEmail, 
	        											  instructorPhone, instructorOfficeBuilding, 
	        											  instructorOfficeNumber, instructorTypeId);
	        	}
	        	else if(extras.getString("action").equals("update")){
	        		instructorDatasource.updateInstructor(instructorId, instructorFirstName, 
	        											  instructorLastName, instructorDepartment, 
	        											  instructorEmail, instructorPhone, 
	        											  instructorOfficeBuilding, instructorOfficeNumber, 
	        											  instructorTypeId);
	        	}
        	}
        	
        	if(extras.getString("action") != null && !extras.getString("action").isEmpty()){
        		if(extras.getString("action").equals("deleteTask")){
        			taskDatasource.deleteTask(extras.getInt("taskId"));
        		}else if(extras.getString("action").equals("deleteClass")){
        			classDatasource.deleteClass(extras.getInt("classId"));
        		}else if(extras.getString("action").equals("deleteInstructor")){
        			instructorDatasource.deleteInstructor(extras.getInt("instructorId"));
        		}
        	}
        }
        
        List<Class> classValues = classDatasource.getAllClasses();
        List<Task> taskValues = taskDatasource.getAllTasks();
        
        ExpandableListAdapter adapter = new ExpandableListAdapter(classValues, taskValues, this);
        
        setListAdapter(adapter); 
        
        classDatasource.close();
        taskDatasource.close();
        instructorDatasource.close();
    }
    
	public void onClick(View v) {
		Intent i;
		switch(v.getId()){
		case R.id.classManagementButton:
			i = new Intent(this, CreateClass.class);
			startActivity(i);
			break;
		case R.id.taskManagementButton:
			i = new Intent(this, CreateTask.class);
			startActivity(i);
			break;
		case R.id.instructorManagementButton:
			classDatasource.clearAll();
			i = new Intent(this, CreateInstructor.class);
			startActivity(i);
			break;		
		}
		
	}
	
    @Override
	protected void onResume() {
		classDatasource.open();
		taskDatasource.open();
		instructorDatasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		classDatasource.close();
		taskDatasource.close();
		instructorDatasource.close();
		super.onPause();
	}		
}